<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Show\Refresh;
use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Carbon;
use SuperEggs\DcatDistpicker\DcatDistpickerHelper;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->scrollbarX();
            $grid->column('id')->sortable();
            $grid->column('username');
            $grid->column('realname');
            // $grid->column('avatar');
            $grid->column('email');
            $grid->column('wechart', '微信号');
            $grid->column('wechart_name', '微信昵称');
            // $grid->column('status');
            $grid->column('last_login_time');

            // $grid->column('sex');
            // $grid->column('birthday');

            // $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('username');
                $filter->equal('realname');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('username');
            $show->field('realname');
            // $show->field('avatar');
            $show->field('email');

            $show->field('phone');
            $show->field('ip');
            $show->status()->using(['0' => '禁用', '1' => '启用']);
            $show->field('last_login_time');
            $show->field('wechart', '微信号');
            $show->field('wechart_name', '微信昵称');
            $show->field('surname', '姓');
            $show->field('name', '名');
            $show->sex()->using(['1' => '男', '2' => '女']);
            $show->field('birthday', '出生日期');
            $show->field('institution', '机构名称')->as(function ($ins) {
                if (!empty($ins)) {
                    return implode(',', json_decode($ins, true));
                } else {
                    return ' - ';
                }
            });
            $show->field('zip_code', '邮编');

            $show->field('province_id', '省')->as(function ($code) {

                return DcatDistpickerHelper::getAreaName($code);
            });
            $show->field('city_id', '市')->as(function ($code) {
                return DcatDistpickerHelper::getAreaName($code);
            });
            $show->field('district_id', '区')->as(function ($code) {

                return DcatDistpickerHelper::getAreaName($code);
            });

            $show->field('created_at');
            $show->field('updated_at');
            $show->tools(function (Show\Tools $tools) {
                $tools->append(new Refresh());
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('username');
            $form->text('realname');
            $form->image('avatar');
            $form->text('email');
            $id = $form->getKey();
            if ($id) {
                $form->password('password', trans('admin.password'))
                    ->minLength(5)
                    ->maxLength(20)
                    ->customFormat(function () {
                        return '';
                    });
            } else {
                $form->password('password', trans('admin.password'))
                    ->required()
                    ->minLength(5)
                    ->maxLength(20);
            }
            $form->mobile('phone');
            $form->slider('age', '年龄')->options(['max' => 100, 'min' => 1, 'step' => 1, 'postfix' => 'years old']);
            $form->radio('status')->options(['0' => '禁用', '1' => '启用'])->default('1');
            $form->text('wechart', '微信号')->required();
            $form->text('wechart_name', '微信昵称')->required();
            $form->select('surname', '称谓')->options([1 => 'Dr.', 2 => 'Mr.', 3 => 'Prof.', 4 => 'Ms.', 5 => 'Mrs.'])->required();
            $form->text('name', '名')->required();

            $form->radio('sex')->options(['1' => '男', '2' => '女'])->default('1');
            $form->date('birthday', '出生日期')->format('YYYY-MM-DD');
            $form->text('institution', '机构名称')->required();
            // $form->keyValue('institution', '机构')->default(['key' => '', 'value' => ''])->setKeyLabel('键名')->setValueLabel('机构名称');
            $form->table('institution_detail', '机构名称', function ($table) {
                // $table->text('key')->default(time())->disable();
                $table->text('value', '机构名称');
            });

            $form->distpicker([
                'province_id' => '----省----',
                'city_id'     => '----市----',
                'district_id' => '----区----'
            ], '所在地');
            $form->text('zip_code', '邮编');
            $form->text('address', '邮寄地址');
            // $form->display('created_at');
            // $form->display('updated_at');
        })->saving(function (Form $form) {
            if ($form->password && $form->model()->get('password') != $form->password) {
                $form->password = bcrypt($form->password);
            }

            if (!$form->password) {
                $form->deleteInput('password');
            }
        });
    }
}
